package com.sgb.member.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.SparseArray;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.sgb.member.R;
import com.sgb.member.databinding.ActivityMemberDemandBinding;
import com.sgb.member.ui.fragment.BiddingRegisterFragment;
import com.swgk.core.base.AppActivity;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;
import com.swgk.core.util.MToast;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;

/**
 * 招标报名
 */
public class BiddingRegistrationActivity extends AppActivity {

    private ActivityMemberDemandBinding mBinding;
    private ArrayList<Fragment> mFragments;

    public static void start(Context context) {
        Intent intent = new Intent(context, BiddingRegistrationActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_member_demand);
        initSearchView();
        initTabLayout();
    }

    private void initTabLayout() {
        mBinding.tabLayout.setVisibility(View.GONE);
        if (mFragments != null && mFragments.size() > 0) return;
        mBinding.emptyView.setVisibility(View.GONE);
        mFragments = new ArrayList<>();
        String[] titles = {"不限"};
        mFragments.add(BiddingRegisterFragment.newInstance(0, new CommonPopEntity("", "不限")));
        SlidingFragmentPagerAdapter adapter = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), mFragments, titles);
        mBinding.vp.setOffscreenPageLimit(titles.length);
        mBinding.vp.setAdapter(adapter);
    }

    private String mSearchText = ""; // 搜索内容
    private final SparseArray<String> mSearchList = new SparseArray<>();

    private void initSearchView() {
        mBinding.activityBar.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        String hint = "请输入关键字比如招标名称、企业名称";
        mBinding.activityBar.etView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        mBinding.activityBar.etView.setHint(hint);
        mBinding.activityBar.etView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (TextUtils.isEmpty(mBinding.activityBar.etView.getText().toString())) {
                        MToast.showToast(getActivity(), hint);
                        return true;
                    }
                    if (mSearchText.equals(mBinding.activityBar.etView.getText().toString())) {
                        MToast.showToast(getActivity(), "请输入不同的搜索内容");
                        return true;
                    }
                    mSearchText = mBinding.activityBar.etView.getText().toString().trim();
                    loadSearchData();
                }
                return false;
            }
        });
        mBinding.activityBar.etView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s) || s.length() == 0) {
                    mBinding.activityBar.tvSearchDelete.setVisibility(View.INVISIBLE);
                } else {
                    mBinding.activityBar.tvSearchDelete.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mBinding.activityBar.tvSearchDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSearchText = "";
                mBinding.activityBar.etView.setText("");
                loadSearchData();
            }
        });
        mBinding.activityBar.tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(mSearchText)) {
                    if (TextUtils.isEmpty(mBinding.activityBar.etView.getText().toString())) {
                        MToast.showToast(getActivity(), hint);
                        return;
                    }
                }
                if (mSearchText.equals(mBinding.activityBar.etView.getText().toString())) {
                    MToast.showToast(getActivity(), "请输入不同的搜索内容");
                    return;
                }
                mSearchText = mBinding.activityBar.etView.getText().toString().trim();
                loadSearchData();
            }
        });
        mBinding.activityBar.tvSearchDelete.setVisibility(View.INVISIBLE);
    }

    public String getSearchText() {
        int tabPosition = 0;
        return mSearchList.get(tabPosition);
    }

    private void loadSearchData() {
        int tabPosition = 0;
        mSearchList.put(tabPosition, mSearchText);
        BiddingRegisterFragment fragment = (BiddingRegisterFragment) mFragments.get(tabPosition);
        fragment.loadData(false);
    }
}
